package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseBaseInfoMapper;
import com.xuecheng.content.mapper.CourseCategoryMapper;
import com.xuecheng.content.mapper.CourseMarketMapper;
import com.xuecheng.content.model.dto.AddCourseDto;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.EditCourseDto;
import com.xuecheng.content.model.po.CourseCategory;
import com.xuecheng.content.model.po.CourseMarket;
import com.xuecheng.content.service.CourseBaseInfoService;
import com.xuecheng.base.model.dto.PageParams;
import com.xuecheng.base.model.dto.PageResult;
import com.xuecheng.content.model.dto.QueryCourseParamsDto;
import com.xuecheng.content.model.po.CourseBase;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: Hes
 * @description 课程信息实现
 * @date: 2023/6/20 0:18
 */
@Service
@Slf4j
public class CourseBaseInfoServiceImpl implements CourseBaseInfoService {
    @Autowired
    CourseBaseInfoMapper courseBaseInfoMapper;
    @Autowired
    CourseMarketMapper courseMarketMapper;

    @Autowired
    CourseCategoryMapper categoryMapper;

    /**
     * 查询课程信息
     *
     * @param params
     * @param queryCourseParamsDto
     * @return
     */
    @Override
    public PageResult<CourseBase> list(PageParams params, QueryCourseParamsDto queryCourseParamsDto) {
        Long pageNo = params.getPageNo();
        Long pageSize = params.getPageSize();

        Page<CourseBase> courseBasePage = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();
        //这种前边判断值是否为空，不能链式编程，会报错
        queryWrapper.like(StringUtils.isNotEmpty(queryCourseParamsDto.getCourseName()), CourseBase::getName, queryCourseParamsDto.getCourseName());
        queryWrapper.eq(StringUtils.isNotEmpty(queryCourseParamsDto.getAuditStatus()), CourseBase::getAuditStatus, queryCourseParamsDto.getAuditStatus());
        queryWrapper.eq(StringUtils.isNotEmpty(queryCourseParamsDto.getPublishStatus()), CourseBase::getStatus, queryCourseParamsDto.getPublishStatus());
        Page<CourseBase> courseBases = courseBaseInfoMapper.selectPage(courseBasePage, queryWrapper);

        long total = courseBases.getTotal();
        long pages = courseBases.getPages();
        long size = courseBasePage.getSize();
        List<CourseBase> records = courseBases.getRecords();
        PageResult<CourseBase> result = new PageResult<CourseBase>(records, pages, size, total);
        return result;
    }

    /**
     * 增加课程，保存课程信息，营销课程，返回课程信息dto
     *
     * @param companyId
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public CourseBaseInfoDto createCourseBase(Long companyId, AddCourseDto dto) {
        CourseBase courseBase = new CourseBase();
        BeanUtils.copyProperties(dto, courseBase);
        courseBase.setStatus("203001");
        courseBase.setAuditStatus("202002");
        courseBase.setCreateDate(LocalDateTime.now());
        courseBase.setCompanyId(companyId);
        int insert = courseBaseInfoMapper.insert(courseBase);
        if (insert <= 0) {
            throw new XueChengPlusException("添加课程失败");
        }
        //插入营销表
        Long courseId = courseBase.getId();
        CourseMarket courseMarket = new CourseMarket();
        BeanUtils.copyProperties(dto, courseMarket);
        courseMarket.setId(courseId);
        saveCourseMarket(courseMarket);
        //查询营销表和课程表信息，返回
        CourseBaseInfoDto courseBaseInfo = getCourseBaseInfo(courseId);
        return courseBaseInfo;
    }

    /**
     * 根据课程id查询课程信息
     *
     * @param courseId
     * @return
     */
    @Override
    public CourseBaseInfoDto getCourseBaseById(Long courseId) {
        CourseBaseInfoDto courseBaseInfoDto = new CourseBaseInfoDto();
        if (courseId == null) {
            XueChengPlusException.cast("课程id为空，不能查询课程信息");
        }
        CourseBase courseBase = courseBaseInfoMapper.selectById(courseId);
        if (courseBase == null) {
            XueChengPlusException.cast("查询课程为空");
        }
        BeanUtils.copyProperties(courseBase, courseBaseInfoDto);
        CourseMarket courseMarket = courseMarketMapper.selectById(courseId);
        if (courseMarket != null) {
            BeanUtils.copyProperties(courseMarket, courseBaseInfoDto);
        }
        //查询分类名称
        CourseCategory courseCategoryBySt = categoryMapper.selectById(courseBase.getSt());
        courseBaseInfoDto.setStName(courseCategoryBySt.getName());
        CourseCategory courseCategoryByMt = categoryMapper.selectById(courseBase.getMt());
        courseBaseInfoDto.setMtName(courseCategoryByMt.getName());

        return courseBaseInfoDto;
    }

    /**
     * @param editCourseDto
     * @return
     */
    @Transactional
    @Override
    public CourseBaseInfoDto modifyCourseBase(Long companyId, EditCourseDto editCourseDto) {

        Long id = editCourseDto.getId();
        CourseBase courseBase = courseBaseInfoMapper.selectById(id);
        if (courseBase == null) {
            XueChengPlusException.cast("课程不存在");
        }
        //更新课程信息表
        BeanUtils.copyProperties(editCourseDto, courseBase);
        courseBaseInfoMapper.updateById(courseBase);
        //准备返回数据
        CourseBaseInfoDto courseBaseInfoDto = new CourseBaseInfoDto();
        BeanUtils.copyProperties(courseBase, courseBaseInfoDto);
        CourseMarket courseMarket = courseMarketMapper.selectById(id);
        //如果营销表不存在这个id，直接返回
        if (courseMarket == null) {
            return courseBaseInfoDto;
        }
        //更新营销表
        BeanUtils.copyProperties(editCourseDto, courseMarket);
        courseMarketMapper.updateById(courseMarket);
        //返回courseBaseInfoDto对象
        BeanUtils.copyProperties(courseMarket, courseBaseInfoDto);
        return courseBaseInfoDto;
    }

    /**
     * 根据课程id查询课程信息dto
     *
     * @param courseId
     * @return
     */
    private CourseBaseInfoDto getCourseBaseInfo(Long courseId) {
        CourseBaseInfoDto courseBaseInfoDto = new CourseBaseInfoDto();
        CourseBase courseBase = courseBaseInfoMapper.selectById(courseId);
        BeanUtils.copyProperties(courseBase, courseBaseInfoDto);
        CourseMarket courseMarket = courseMarketMapper.selectById(courseId);
        BeanUtils.copyProperties(courseMarket, courseBaseInfoDto);
        return courseBaseInfoDto;
    }

    /**
     * 添加课程营销信息
     *
     * @param courseMarket
     */
    public void saveCourseMarket(CourseMarket courseMarket) {


        String charge = courseMarket.getCharge();
        if (StringUtils.isEmpty(charge)) {
            throw new XueChengPlusException("收费规则没有选择");
        }

        //收费规则为收费
        if (charge.equals("201001")) {
            if (courseMarket.getPrice() == null || courseMarket.getPrice().floatValue() <= 0) {
                throw new XueChengPlusException("课程为收费价格不能为空且必须大于0");
            }
        }
        CourseMarket courseMarketObj = courseMarketMapper.selectById(courseMarket);
        if (courseMarketObj == null) {
            int insert = courseMarketMapper.insert(courseMarket);
            if (insert <= 0) {
                throw new XueChengPlusException("添加课程营销信息失败");
            }
        } else {
            BeanUtils.copyProperties(courseMarket, courseMarketObj);
            courseMarketMapper.updateById(courseMarketObj);
        }


    }


}
